package com.winit.commons.wsAgent.util;

import com.alibaba.fastjson.JSONObject;
import com.winit.commons.util.DateUtil;
import com.winit.commons.util.HttpClientUtil;
import com.winit.commons.util.ObjectToString;
import com.winit.commons.wsAgent.bean.AgentMenuUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Created by wangjian on 2017/11/14.
 */
public class AgentMenuUtil {

    private static Logger log = LoggerFactory.getLogger(AgentMenuUtil.class);

    /**
     * 修改菜单
     *
     * @param ws_uuid
     * @param menu
     * @return
     * @throws Exception
     */
    public static JSONObject update(String ws_uuid, AgentMenuUpdate menu) throws Exception {
        String ip = ws_uuid.split("_")[0];
        String port = ws_uuid.split("_")[1];
        String workspace = ws_uuid.split("_")[2];
        menu.setWorkspace(workspace);
        StringBuffer url = new StringBuffer();
        url.append("http://").append(ip).append(":").append(port).append("/api/v1/menu/update?")
                .append(ObjectToString.getUrlString(menu));
        JSONObject jsonObject = HttpClientUtil.doGetForJson(url.toString());
        log.info("com.winit.commons.wsAgent.util.AgentMenuUtil.update ws_uuid=" + ws_uuid + ".menu=" + ObjectToString.getString(menu) + ".url=" + url + ".rtn=" + jsonObject);
        return jsonObject;
    }

    /**
     * 删除菜单
     *
     * @param ws_uuid
     * @param menu    只需要menu_id 和 workspace
     * @return
     * @throws Exception
     */
    public static JSONObject delete(String ws_uuid, AgentMenuUpdate menu) throws Exception {
        String ip = ws_uuid.split("_")[0];
        String port = ws_uuid.split("_")[1];
        String workspace = ws_uuid.split("_")[2];
        StringBuffer url = new StringBuffer();
        url.append("http://").append(ip).append(":").append(port).append("/api/v1/menu/delete?menu_id=")
                .append(menu.getMenu_id()).append("&workspace=").append(workspace);
        JSONObject jsonObject = HttpClientUtil.doGetForJson(url.toString());
        log.info("com.winit.commons.wsAgent.util.AgentMenuUtil.delete ws_uuid=" + ws_uuid + ".menu=" + ObjectToString.getString(menu) + ".url=" + url + ".rtn=" + jsonObject);
        return jsonObject;
    }

    public static void main(String[] args) throws Exception {
        AgentMenuUpdate menu = new AgentMenuUpdate();
        menu.setParent_menu_id("-1");
        menu.setMenu_id("m1");
        menu.setTitle("");
        menu.setIndex(0);
        menu.setPath("");
        System.out.println(ObjectToString.getUrlString(menu));
    }
}
